import bpy
import math
from mathutils import Vector
import random

# 设置相机数量和圆周半径
num_cams = 10
num_high = 3
radius = 3

# 获取场景中的相机对象
camera = bpy.data.objects['Camera']

# 获取场景中的物体对象
obj = bpy.data.objects['Honda pc 50 1969']

# 得到物体的中心点位置
center = Vector(obj.location)
center.z += 0.5

camera_lens = [47, 57 ,68]


# 循环生成不同的相机位置和指向
for i in range(num_cams):
    # 随机生成相机位置
#    angle = random.uniform(0, 2 * math.pi)
    angle = 2 * math.pi * i / num_cams
    x = radius * math.cos(angle)
    y = radius * math.sin(angle)
    for j in range(num_high):
        z = j + 2  # 垂直偏移量
        location = (x, y, z)
        camera_location = Vector(location)
        print(center)
        print(camera_location)
        # 计算相机指向物体的方向向量
        camera_direction = (center - camera_location).normalized()
        print(camera_direction)
        # 设置相机位置和旋转角度
        camera.rotation_mode = 'XYZ'
        camera.location = camera_location
        camera.rotation_euler = camera_direction.to_track_quat('-Z', 'Y').to_euler()
        #camera.rotation_euler.z += math.radians(90)  # 调整相机的仰角
        camera.data.lens = camera_lens[j]
        print(camera.data.lens)

        # 设置渲染参数
        bpy.context.scene.camera = camera
        bpy.context.scene.render.filepath = f'D:\\pictures\\{i*3+j+1}.png'
        bpy.context.scene.render.resolution_x = 512
        bpy.context.scene.render.resolution_y = 512
        bpy.ops.render.render(write_still=True)
